package edu.ahpu.propertymanage.controller;

import edu.ahpu.propertymanage.dataclass.UserLogin;
import edu.ahpu.propertymanage.pojo.Managers;
import edu.ahpu.propertymanage.pojo.Owner;
import edu.ahpu.propertymanage.pojo.Worker;
import edu.ahpu.propertymanage.service.ManagersService;
import edu.ahpu.propertymanage.service.OwnerService;
import edu.ahpu.propertymanage.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    OwnerService ownerService;

    @Autowired
    ManagersService managersService;

    @Autowired
    WorkerService workerService;

    @PostMapping("/dologin")
    public String doLogin(@RequestBody UserLogin userLogin) {
        if ("1".equals(userLogin.getRadio())) {
            Owner owner = ownerService.getById(userLogin.getUname());
            if (owner != null && userLogin.getPassword().equals(owner.getOwnerPw())) {

                return "success-owner";
            } else
                return "error";

        } else if ("2".equals(userLogin.getRadio())) {
            Managers managers = managersService.getById(userLogin.getUname());
            if (managers != null && userLogin.getPassword().equals(managers.getManagerPw())) {
                if (managers.getManagerPow() == 5)
                    return "SuperlativeManage";
                else
                    return "success-manager";
            } else
                return "error";
        } else if ("3".equals(userLogin.getRadio())) {
            Worker worker = workerService.getById(userLogin.getUname());
            if (worker != null && userLogin.getPassword().equals(worker.getWorkerPw()))
                return "success-worker";
            else
                return "error";
        } else
            return "error";
    }

    @PostMapping("/owner")
    public String findOwnername(String userId) {
        Owner owner = ownerService.getById(userId);
        return owner.getOwnerName();
    }

    @PostMapping("/manager")
    public Managers findManager(String userId) {
        Managers managers = managersService.getById(userId);
        System.out.println(managers);
        return managers;
    }

    @PostMapping("/worker")
    public String findWorker(String userId) {
        Worker worker = workerService.getById(userId);
        return worker.getWorkerName();
    }

    @PostMapping("/workerstar")
    public Float workerStar(String userId){
        Worker worker = workerService.getById(userId);
        return worker.getWorkerStar();
    }
}
